package com.york.utils.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.york.utils.exception.result.R;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yangboxiong
 */
public class ResponseUtil {

    /**
     * 完全退出处理
     *
     * @param response
     * @param r
     */
    public static void out(HttpServletResponse response, R r) {
        //创建对象映射器
        ObjectMapper mapper = new ObjectMapper();
        //设置 http 状态码
        response.setStatus(HttpStatus.OK.value());
        //设置设置编码格式
        response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
        try {
            //直接将传⼊的对象序列化为json，并且返回给客户端，包含了响应的字符串和响应值
            mapper.writeValue(response.getWriter(), r);
            mapper.writeValueAsString("你好");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
